//
//  MainViewController.m
//  LPSNews
//
//  Created by Ruan on 14-10-10.
//  Copyright (c) 2014年 LPS. All rights reserved.
//

#import "MainViewController.h"
#import "NewsViewController.h"
#import "SettingViewController.h"
#import "PictureViewController.h"
#import "VideoViewController.h"
#import "CollectViewController.h"
#import "UIButton+NavButton.h"
#import "RootViewDisplayControllerDelegate.h"
#import "NewsViewControllerDelegate.h"
#import "CollectViewControllerDelegate.h"
#import "PhotoViewController.h"
#import "SettingViewController.h"
#import "SettingViewControllerDelegate.h"
#import "SDWebImageManager.h"
#import "FilePathManager.h"
//视屏
#define Media_Play @"http://c.m.163.com/nc/video/list/V9LG4B3A0/n/0-10.html"
#define HOT_URL @"http://c.m.163.com/photo/api/list/0096/54GI0096.json"
@interface MainViewController ()<RootViewDisplayControllerDelegate,NewsViewControllerDelegate,CollectViewControllerDelegate,PictureViewControllerDelegate,SettingViewControllerDelegate,UIAlertViewDelegate>


@property (nonatomic, retain) CollectViewController *collectVC;
@property (nonatomic, retain)  UINavigationController *collectNav;
@property (nonatomic, retain) VideoViewController * videoVC;
@property (nonatomic, retain) PictureViewController *pictureVC;
@property (nonatomic, retain) SettingViewController *subscribeVC;
@end

@implementation MainViewController


- (void)dealloc
{
    
    [_collectVC release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.title = @"LPSNews";
    // 左侧菜单的Button
    UIButton *navLeftButton = [UIButton addNavButtonWithNormalImageString:@"top_navigation_menuicon" andHeightLightedImageString:@"top_navigation_menuicon_highlighted"];
    [navLeftButton addTarget:self action:@selector(leftBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 右侧设置的Button
    UIButton *userInroButton = [UIButton addNavButtonWithNormalImageString:@"setting_icon_normal" andHeightLightedImageString:@"setting_icon_highlight"];
    
    [userInroButton addTarget:self action:@selector(settingClick) forControlEvents:UIControlEventTouchUpInside];
    userInroButton.contentEdgeInsets = UIEdgeInsetsMake(2, 0, -2, 0);
    
    // 向导航条上添加左侧BarButton
    UIBarButtonItem *leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:navLeftButton] autorelease];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    // 向导航条上添加右侧BarButton
    UIBarButtonItem *newsRightBarButton = [[[UIBarButtonItem alloc] initWithCustomView:userInroButton] autorelease];
    self.navigationItem.rightBarButtonItem = newsRightBarButton;
    
    
    // 1.新闻页控制器
    NewsViewController *newsVC = [[NewsViewController alloc] init];
    newsVC.displayDelegate = self;
    // 2.图片页控制器
    PictureViewController *pictureVC = [[PictureViewController alloc] initWithStyle:UITableViewStylePlain];
    self.pictureVC = pictureVC;
    pictureVC.delegate = self;
    // 3.视频页控制器
    VideoViewController *videoVC = [[VideoViewController alloc] initWithStyle:UITableViewStylePlain];
    self.videoVC = videoVC;
    
    // 4.收藏控制器
    CollectViewController *collectVC = [[CollectViewController alloc] initWithStyle:UITableViewStylePlain];
    self.collectVC = collectVC;
    collectVC.delegate = self;
    
    [self addChildViewController:newsVC];
    [self addChildViewController:pictureVC];
    [self addChildViewController:videoVC];

    
    
    [self.view addSubview:newsVC.view];
    
    [newsVC release];
    [pictureVC release];
    [videoVC release];
    [collectVC release];

}



// 点击左侧的BarButton 让RootView显示
- (void)leftBarButtonClick:(UIButton *)btn
{
    if([_delegate respondsToSelector:@selector(displayRootView)])
    {
        [_delegate displayRootView];
    }
}


// 设置点击
- (void)settingClick
{
    
    SettingViewController *subscribeVC = [[SettingViewController alloc] init];

    
    
    [self.navigationController pushViewController:subscribeVC animated:YES];
    
    
    [subscribeVC release];
}



// 通过控制面板按钮的Tag值的改变,传给Main控制器的数组元素的索引,通过索引来引导显示的页面
- (void)displayControllerFromIndex:(int)fromIndex ToIndex:(int)toIndex andTitleText:(NSString *) titleText
{
  
    // 如果点击相同直接返回
    if (fromIndex == toIndex) {
        
        return;
    }
    
    if (toIndex == 0) {
        self.navigationItem.title = @"LPSNews";
        
    }else if(toIndex == 1){
        
        self.navigationItem.title = @"图片";
        [self.pictureVC requestWebDataForURLString:HOT_URL andIsDowndrag:YES];
        
    }else if(toIndex == 2){
        
        self.navigationItem.title = @"视频";
        [self.videoVC tableViewDownLoadDataForURLString:Media_Play andIsDowndrag:YES];
    
    }
    else if (toIndex == 3)
    {
        [self.collectVC addCollectDate];
        [self newsViewControllerCancelSwipeGesture];
        [self.navigationController pushViewController:self.collectVC animated:YES];
        return;
      
    }else if (toIndex == 4)
    {
        SDImageCache *imageCache = [SDImageCache sharedImageCache];
        
        [imageCache clearDisk];

        
        NSFileManager *fm = [NSFileManager defaultManager];
        
        NSArray *listFileArray = [fm subpathsAtPath:[FilePathManager webCachePath]];
        
        for (NSString *fileStr in listFileArray) {
            
            [fm removeItemAtPath:[FilePathManager imagePathCache:fileStr] error:nil];
        }
        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"内存已清理" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
        UITableViewController *tableFrom = self.childViewControllers[fromIndex];
        [tableFrom.view removeFromSuperview];
        
        // 添加新的View
        UITableViewController *tableTo = self.childViewControllers[0];
        [self.view addSubview:tableTo.view];
        
        return;
    }
    
    // 移除当前显示的View
    if (fromIndex < 3) {
        UITableViewController *tableFrom = self.childViewControllers[fromIndex];
        [tableFrom.view removeFromSuperview];
        
        // 添加新的View
        UITableViewController *tableTo = self.childViewControllers[toIndex];
        [self.view addSubview:tableTo.view];
    }
   
}


// 显示rootView
- (void)newsViewControllerDisplayPanChangePointX:(CGFloat)rootViewX
{
    if (_displayDeleage &&[_displayDeleage respondsToSelector:@selector(mainViewControllerDisplayPanChangePointX:)]) {
        [_displayDeleage mainViewControllerDisplayPanChangePointX:rootViewX];
    }
    
}


// 平移rootVIEW
- (void)newsViewControllerDisplayPanStateEnd
{
    if (_displayDeleage && [_displayDeleage respondsToSelector:@selector(mainViewControllerDisplayPanStateEnd)]) {
        [_displayDeleage mainViewControllerDisplayPanStateEnd];
    }
}


// delegate的传值
- (void)newsViewControllerCancelSwipeGesture
{
    
    if (_displayDeleage && [_displayDeleage respondsToSelector:@selector(mainViewControllerCancelSwipeGesture)])
    {
        [_displayDeleage mainViewControllerCancelSwipeGesture];
    }
    
}

// delegate的传值
- (void)newsViewControllerOpenSwipeGesture
{
    if (_displayDeleage && [_displayDeleage respondsToSelector:@selector(mainViewControllerOpenSwipeGesture)])
    {
        [_displayDeleage mainViewControllerOpenSwipeGesture];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// delegate的传值
- (void)collectViewControllerSelectRootViewFirstIndex
{
    
    [self newsViewControllerOpenSwipeGesture];
    if (_displayDeleage && [_displayDeleage respondsToSelector:@selector(mainViewControllerSelectRootViewFirstIndex)]) {
        [_displayDeleage mainViewControllerSelectRootViewFirstIndex];
    }
}

// delegate的传值
- (void)pictureViewControllerCancelSwipeGesture
{
    [self newsViewControllerCancelSwipeGesture];
}


 - (void)pictureViewControllerOpenSwipeGesture
{
}

// delegate的传值
- (void)settingViewControllerSelectRootViewFirstIndex
{
    if (_displayDeleage && [_displayDeleage respondsToSelector:@selector(mainViewControllerSelectRootViewFirstIndex)]) {
        [_displayDeleage mainViewControllerSelectRootViewFirstIndex];
    }

}


// alert提醒
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self collectViewControllerSelectRootViewFirstIndex];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
